﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WPLDB.DA.Interfaces;
using WPLDB.Utility.Extensions;

namespace WPLDB.Code
{
    /// <summary>
    /// This class helps to created base class for tables C# code 
    /// and variables naming are not explicit required the explanations
    /// </summary>
    public class CreateTableBaseClassCode : ICreateClassCode
    {
        #region "Constant"

        string CLASS = "[NEWLINE][COMMENT1][NEWLINE][T]public class TableBase : INotifyPropertyChanged, INotifyPropertyChanging[NEWLINE]" +
                        "[T]{[NEWLINE]" +
                        "[NEWLINE]" +
                        "[T][T]public event PropertyChangingEventHandler PropertyChanging;[NEWLINE]" +
                        "[NEWLINE]" +
                        "[T][T]public event PropertyChangedEventHandler PropertyChanged;[NEWLINE]" +
                        "[NEWLINE]" +
                        "[T][T]public void OnPropertyChanged<T>(Expression<Func<T>> expression)[NEWLINE]" +
                        "[T][T]{[NEWLINE]" +
                        "[T][T][T]if (null != PropertyChanged)[NEWLINE]" +
                        "[T][T][T]{[NEWLINE]" +
                        "[T][T][T][T]PropertyChanged(this, new PropertyChangedEventArgs(((MemberExpression)expression.Body).Member.Name));[NEWLINE]" +
                        "[T][T][T]}[NEWLINE]" +
                        "[T][T]}[NEWLINE]" +
                        "[NEWLINE]" +
                        "[T][T]public void OnPropertyChanging<T>(Expression<Func<T>> expression)[NEWLINE]" +
                        "[T][T]{[NEWLINE]" +
                        "[T][T][T]if (null != PropertyChanging)[NEWLINE]" +
                        "[T][T][T]{[NEWLINE]" +
                        "[T][T][T][T]PropertyChanging(this, new PropertyChangingEventArgs(((MemberExpression)expression.Body).Member.Name));[NEWLINE]" +
                        "[T][T][T]}[NEWLINE]" +
                        "[T][T]}[NEWLINE]" +
                        "[NEWLINE]" +
                        "[T][T]public void OnPropertyChanged(string propertyName)[NEWLINE]" +
                        "[T][T]{[NEWLINE]" +
                        "[T][T][T]if (null != PropertyChanged)[NEWLINE]" +
                        "[T][T][T]{[NEWLINE]" +
                        "[T][T][T][T]PropertyChanged(this, new PropertyChangedEventArgs(propertyName));[NEWLINE]" +
                        "[T][T][T]}[NEWLINE]" +
                        "[T][T]}[NEWLINE]" +
                        "[NEWLINE]" +
                        "[T][T]public void OnPropertyChanging(string propertyName)[NEWLINE]" +
                        "[T][T]{[NEWLINE]" +
                        "[T][T][T]if (null != PropertyChanging)[NEWLINE]" +
                        "[T][T][T]{[NEWLINE]" +
                        "[T][T][T][T]PropertyChanging(this, new PropertyChangingEventArgs(propertyName));[NEWLINE]" +
                        "[T][T][T]}[NEWLINE]" +
                        "[T][T]}[NEWLINE]" +
                        "[NEWLINE]" +
                        "[T][T]public void set<T, K>(Expression<Func<T>> expresson, ref K _variable, K value)[NEWLINE]" +
                        "[T][T]{[NEWLINE]" +
                        "[T][T][T]if (_variable == null || !_variable.Equals(value))[NEWLINE]" +
                        "[T][T][T]{[NEWLINE]" +
                        "[T][T][T][T]OnPropertyChanging(expresson);[NEWLINE]" +
                        "[T][T][T][T]_variable = value;[NEWLINE]" +
                        "[T][T][T][T]OnPropertyChanged(expresson);[NEWLINE]" +
                        "[T][T][T]}[NEWLINE]" +
                        "[T][T]}[NEWLINE]" +
                        "[T]}[NEWLINE]";

        string COMMENT1 = "[T]/// <summary>[NEWLINE]" +
                         "[T]/// Base class for all the table classes and it handling property change events.[NEWLINE]" +
                         "[T]/// </summary>";

        #endregion

        #region "Interface Implementation"

        public string GenerateClassString()
        {
            return CLASS
                .ReplaceComment(COMMENT1, 1)
                .ReplaceNewLine();
        }

        #endregion

    }
}
